#!/bin/bash
## load images from docker-registry
images=(zookeeper kafka mysql postgres mongo memcached redis elk rabbitmq kibana httpd-php)
for image in ${images[@]}
do
    is_exists=$(docker images | grep ${image})
    if [ ! "$is_exists" ]; then
        echo 'pulling image ${image}';
        docker pull $image
    fi
done

## load images for location
for file in ./images/*
do
    if test -f $file
    then
        echo "Loading Image:$file"
        docker load -i ${file}
    fi
done

## build
name=devops
passwd=root

result=$(docker ps -a | grep "$name-")
if [ ! "$result" ]; then
    # run   --restart=always
    docker run --restart=always --name $name-zookeeper -p 2181:2181 -p 2888:2888 -p 3888:3888 -v "$PWD/zookeeper/conf":/conf -v "$PWD/zookeeper/data":/data -d zookeeper
    docker run --restart=always --name $name-kafka -p 9092:9092 -v /var/run/docker.sock:/var/run/docker.sock --link $name-zookeeper -e KAFKA_CREATE_TOPICS="$name:1:1" -e KAFKA_ZOOKEEPER_CONNECT="$name-zookeeper:2181" -e KAFKA_ADVERTISED_HOST_NAME="$name-kafka" -d kafka
    docker run --restart=always --name $name-mysql -p 3306:3306 -v "$PWD/mysql/conf":/etc/mysql -v "$PWD/mysql/data":/var/lib/mysql -e MYSQL_ROOT_PASSWORD=$passwd -d mysql
    docker run --restart=always --name $name-postgres -v "$PWD/postgres/data":/var/lib/postgresql/data -e POSTGRES_PASSWORD=$passwd -d postgres
    docker run --restart=always --name $name-mongo -v "$PWD/mongo/configdb":/data/configdb -v "$PWD/mongo/db":/data/db -d mongo  --storageEngine wiredTiger --auth
    docker run --restart=always --name $name-memcached -d memcached
    docker run --restart=always --name $name-redis -v "$PWD/redis/conf":/usr/local/etc/redis -v "$PWD/redis/data":/data -d redis
    docker run --restart=always --name $name-rabbitmq -v "$PWD/rabbitmq/data":/var/lib/rabbitmq -e RABBITMQ_ERLANG_COOKIE=devops-rabbitmq -e RABBITMQ_DEFAULT_USER=rabbitmq -e RABBITMQ_DEFAULT_PASS=$passwd -d rabbitmq
    docker run --restart=always --name $name-elk -p 5601:5601 -p 9200:9200 -p 5044:5044 \
            -v "$PWD/elasticsearch/etc":/etc/elasticsearch \
            -v "$PWD/elasticsearch/config":/opt/elasticsearch/config \
            -v "$PWD/elasticsearch/data":/var/lib/elasticsearch \
            -v "$PWD/elasticsearch/log":/var/log/elasticsearch \
            -v "$PWD/elasticsearch/plugins":/opt/elasticsearch/plugins \
            -v "$PWD/logstash/etc":/etc/logstash \
            -v "$PWD/logstash/config":/opt/logstash/config \
            -v "$PWD/logstash/data":/opt/logstash/data \
            -v "$PWD/logstash/log":/var/log/logstash \
            -v "$PWD/kibana/config":/opt/kibana/config \
            -v "$PWD/kibana/plugins":/opt/kibana/plugins \
            -v "$PWD/kibana/data":/opt/kibana/data \
            -v "$PWD/kibana/log":/var/log/kibana \
            -d elk:5.6.1
    docker run --restart=always --name $name-adminer -p 8080:8080 \
        --link $name-mysql:$name-mysql \
        --link $name-mysql-slave-1:$name-mysql-slave-1 \
        --link $name-mysql-slave-2:$name-mysql-slave-2 \
        --link $name-postgres:$name-postgres \
        --link $name-mongo:$name-mongo \
        --link $name-memcached:$name-memcached \
        --link $name-redis:$name-redis \
        --link $name-elk:$name-elk \
        --link $name-rabbitmq:$name-rabbitmq \
        -e ADMINER_DESIGN='nette' \
        -d adminer
else
    echo "Devops Already Exists!"
    exit 0
fi
